package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.pojo.User;
import com.kuang.utils.JsonUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: SpringMVC
 * @description:
 * @author: 陈恩涛
 * @create: 2020-08-03 06:28
 **/
@RestController
//@Controller
public class UserController {

    @RequestMapping("/j1")
    //@ResponseBody //他就不会走视图解析器，会直接返回一个字符串
    public String json1() throws JsonProcessingException {

        //jackson. ObjectMapper
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();

        //创建一个对象
        User user = new User("小宝", 18, "男");

        //将我们的对象解析成为json格式
        return mapper.writeValueAsString(user);
    }


    @RequestMapping("/j2")
    //@ResponseBody //他就不会走视图解析器，会直接返回一个字符串
    public String json2() throws JsonProcessingException {


        List<User> userList = new ArrayList<User>();

        User user1 = new User("小宝1", 18, "男");
        User user2 = new User("小宝2", 18, "男");
        User user3 = new User("小宝3", 18, "男");
        User user4 = new User("小宝4", 18, "男");
        User user5 = new User("小宝5", 18, "男");
        User user6 = new User("小宝6", 18, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);

        return JsonUtils.getJson(userList);
    }

    @RequestMapping("/j3")
    public String json3() throws JsonProcessingException {


        Date date = new Date();

        return JsonUtils.getJson(date,"yyyy-MM-dd HH:mm:ss");

    }

    @RequestMapping("/j4")
    //@ResponseBody //他就不会走视图解析器，会直接返回一个字符串
    public String json4() throws JsonProcessingException {


        List<User> userList = new ArrayList<User>();

        User user1 = new User("小宝1", 18, "男");
        User user2 = new User("小宝2", 18, "男");
        User user3 = new User("小宝3", 18, "男");
        User user4 = new User("小宝4", 18, "男");
        User user5 = new User("小宝5", 18, "男");
        User user6 = new User("小宝6", 18, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);

        //java对象转JSON字符串
        String str1 = JSON.toJSONString(userList);
        System.out.println(str1);
        String str2 =JSON.toJSONString(user1);
        System.out.println(str2);

        System.out.println("=========================================");
        //JSON字符串转Java对象
        User jp_user1 = JSON.parseObject(str2, User.class);
        System.out.println(jp_user1);

        System.out.println("=========================================");
        //java对象转JSON对象
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println(jsonObject1);
        System.out.println(jsonObject1.getString("name"));

        System.out.println("=========================================");
        //JSON对象转Java对象
        User to_java_user = JSON.toJavaObject(jsonObject1,User.class);
        System.out.println(to_java_user);

        String string = JSON.toJSONString(userList);
        return string;
    }
}

